package com.yisa.kuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.yisa.kuang.pojo.grade;
import com.yisa.kuang.pojo.kuang;
import com.yisa.kuang.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@ResponseBody +@Controller   返回Json格式
//@RestController
//restful风格
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 是一个视图名称
     * 获取数据使用GetMapping()
     *
     * @return
     */
    @GetMapping("/yisa")
    public String list(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,kuang kuan) {
        PageHelper.startPage(pageNum, 5);
        List<kuang> sansan = studentService.findStudengmr(kuan);
        PageInfo<kuang> pageInfo = new PageInfo<kuang>(sansan);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("sansan", sansan);
        return "list";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/student/{id}")
    public String delete(@PathVariable("id") Integer id) {
        studentService.detStudent(id);

        return "redirect:/yisa";
    }

    /**
     * 新增
     *
     * @param kuan
     * @return
     */
    @RequestMapping("/addStudent")
    public String addStudent(kuang kuan) {

        studentService.addStudent(kuan);

        return "redirect:/yisa";
    }

    /**
     * 跳转到新增页面
     *
     * @return
     */
    @RequestMapping("/addHtml")
    public String addHtml() {

        return "add";
    }

    /**
     * 跳转到修改页面
     *
     * @return
     */
    @RequestMapping("/uptHtml/{id}")
    public String uptHtml(@PathVariable("id") Integer gradeId, Model model) {
        kuang kuan = new kuang();
        kuan.setGradeId(gradeId);
        model.addAttribute("model", studentService.findStudent(gradeId));
        return "upt";
    }

    /**
     * 修改
     *
     * @param kuan
     * @return
     */
    @RequestMapping("/uptStudent")
    public String uptStudent(kuang kuan) {
        studentService.updateStudent(kuan);
        return "redirect:/yisa";
    }

    @RequestMapping("/edit")
    public String edit(Integer id ,Model model){
        if (id!=null){
            kuang sansan = studentService.findStudent(id);
            model.addAttribute("sansan",sansan);
        }
        return "edit";
    }

    @RequestMapping("/gmf")
    public  String gmf(kuang kuan, Model model){

//        kuang sgmr = studentService.findStudengmr(kuan, grade);
//        sgmr.setGradeName("");
//        sgmr.setName("");
//        sgmr.setSex("");
        return "redirect:/yisa";
    }

}
